package com.question.controller.admin;

import com.question.pojo.result.PageResult;
import com.question.pojo.result.Result;
import com.question.pojo.dto.*;
import com.question.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/admin/manage/users")
@Slf4j
@Api(tags = "用户管理相关接口")
public class adminUserController {

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    @ApiOperation("用户分页查询")
    public Result<PageResult> getUserPage(@Valid UserPageDTO dto) {
        return Result.success(userService.getUserPage(dto));
    }

    @GetMapping("/stats")
    @ApiOperation("获取用户统计数据")
    public Result<Map<String, Object>> getUserStatistics() {
        return Result.success(userService.getUserStatistics());
    }

    @GetMapping("/{userId}/statistics")
    @ApiOperation("获取单个用户的行为统计")
    public Result<UserStatisticsDTO> getUserBehaviorStatistics(@PathVariable Long userId) {
        return Result.success(userService.getUserBehaviorStatistics(userId));
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    public Result<Void> updateUser(@RequestBody UserDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.success();
    }

    @PutMapping("/password")
    @ApiOperation("重置用户密码")
    public Result<Void> resetPassword(@RequestBody PasswordResetDTO dto) {
        userService.resetPassword(dto.getUserId(), dto.getNewPassword());
        return Result.success();
    }

    @PutMapping("/admin-status")
    @ApiOperation("切换管理员状态")
    public Result<Void> toggleAdminStatus(@RequestBody AdminStatusDTO dto) {
        userService.toggleAdminStatus(dto.getUserId(), dto.getIsAdmin());
        return Result.success();
    }
}